from time import sleep
from unittest import TestCase
from selenium import webdriver

from po_study.dao.MySQLHelper import MySQLHelper
from po_study.po.GoodsListPage import GoodsListPage
from po_study.po.LoginPage import LoginPage
from po_study.utils.Config import Config


class TestEshopGoodList(TestCase):
    file = './config/config.ini'  # 文件路径为自己的包路径
    config = Config(file)

    #定义一个浏览器控件
    def setUp(self) -> None:
        self.driver = webdriver.Chrome(self.config.get_value(self.file,'system','driver'))

    # 定义一个登陆
    def login(self):
        login = LoginPage(self.driver)
        login.open(self.config.get_value(self.file,'system','url'))
        login.input_username()
        login.input_password()
        login.click_login_button()
        sleep(3)

    # 定义一个进入商品列表
    def good_list(self):
        goods = GoodsListPage(self.driver)
        goods.into_menu_frame()
        goods.click_good_list()
        goods.out_frame()
        goods.into_main_frame()
        return goods

    # 定义一个进入添加新商品页
    def goods_add(self):
        goods = GoodsListPage(self.driver)
        goods.into_menu_frame()
        goods.add_new_good()
        goods.out_frame()
        goods.into_main_frame()
        return goods

    def test_select_by_keyword(self):
        # 测试关键词进行搜索
        self.login()
        goods=self.good_list()

        goods.input_search_text()
        #　输入华为
        goods.click_search_button()
        sleep(1)

        # 验证测试结果是否正确
        result = goods.get_search_count()
        sleep(1)

        mysql = MySQLHelper()
        sql = "select count(*) from ecs_goods where goods_name like '%华为%'"
        rs = mysql.select(sql, 'one')[0]

        self.assertEqual(int(result), rs, '测试失败')

    def test_select_by_text(self):
        # 测试通过下拉列表进行搜索
        self.login()
        goods = self.good_list()

        goods.select_by_type()
        goods.click_search_button()
        sleep(5)

        result = goods.get_search_count()

        mysql = MySQLHelper()
        sql = "select count(*) from ecs_goods where is_new=1"
        rs = mysql.select(sql, 'one')
        self.assertEqual(int(result), rs[0], '测试失败')

    def test_add_goods(self):
        # 测试添加新商品
        self.login()

        mysql = MySQLHelper()
        sql = "select count(*) from ecs_goods where goods_name ='联想笔记本'"
        old_result = mysql.select(sql, 'one')[0]

        goods=self.goods_add()
        goods.input_good_name()
        goods.select_good_by_type()
        goods.click_sure()
        sleep(5)

        sql = "select count(*) from ecs_goods where goods_name ='联想笔记本'"
        new_result = mysql.select(sql, 'one')[0]
        self.assertEqual(new_result, old_result + 1, '测试失败')

    def test_is_not_sale(self):
        # 测试是否下架
        mysql = MySQLHelper()
        sql = "update ecs_goods set is_on_sale=1 where goods_id=(select max_id from (select max(goods_id) max_id from ecs_goods) as a)"
        mysql.dml(sql)
        sleep(1)

        self.login()

        goods = self.good_list()
        goods.click_is_or_no()

        sleep(2)

        sql = "select is_on_sale from ecs_goods where goods_id =(select max_id from (select max(goods_id) max_id from ecs_goods) as a)"
        new_result = mysql.select(sql, 'one')[0]
        self.assertEqual(new_result, 0, '测试失败')